package main;

import hypergraph.HyperGraph;
import parse.Constants;
import parse.Parse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStreamReader;


public class proof implements Constants {

	public static void main(String[] args) throws Exception {
				
		String fileName=null;
		try{
			File dir = new File( HG_DIRECTORY );
			
		    FilenameFilter filter = new FilenameFilter() {
		        public boolean accept(File dir, String name) {
		            return name.contains(".hg");
		        }
		    };
		   	String[] files = dir.list( filter );
		    
		    if( files == null ){
		    	throw new Exception("Can not find hg files directory!");
		    } else {
		        for (int i=0; i<files.length; i++) {

		        	String cleanName = files[i].substring(0, files[i].lastIndexOf(".hg"));
		        	HyperGraph hGraph = Parse.readHyperGraph(  HG_DIRECTORY + files[i]  );
					
					int limit = hGraph.minPath();
					
					System.out.println( "ARCHIVO: " + files[i] );
					System.out.println( "Primer cota: " + limit );
					
					hGraph.CreateHyperGraphDotFile( cleanName );
					hGraph.revert();
								
					System.out.println( "Camino Minimo: " + hGraph.getMinPath(limit+1)+ "\n" );
					hGraph.CreateMinHgFile(cleanName);
		        	
		        }
		        // Just for Windows!!
//		        CreateImagesfromDot();
		    }
		} catch (Exception e) {
			System.out.println( "Invalid File Data:\n" + e.getMessage() + " at " + fileName + ".hg");
		}
		
		System.out.println("PROGRAM TERMINATED, Results in: /Tests\n");
		System.out.println("Usar graphviz.sh para obtener la representacion grafica en Tests/files_jpg\n");
		
		return;
	}

	static void CreateImagesfromDot(){
		
		String fileName=null;
		try{
			File dir = new File( DOT_DIRECTORY );
			
		    FilenameFilter filter = new FilenameFilter() {
		        public boolean accept(File dir, String name) {
		            return name.contains(".dot");
		        }
		    };
		   	String[] files = dir.list( filter );
		    
		    if( files == null ){
		    	throw new Exception("Can not find DOT files directory!");
		    } else {
		        for (int i=0; i<files.length; i++) {

		        	String cleanName = files[i].substring(0, files[i].lastIndexOf(".dot"));
		            try {
		    	        Runtime rt = Runtime.getRuntime();
		    	        Process pr = rt.exec("cmd /c  dot -T jpg -o " + JPG_DIRECTORY + cleanName + ".jpg " + DOT_DIRECTORY + cleanName + ".dot" );
		    	        
		    	
		    	        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
		    	
		    	        String line=null;
		    	
		    	        while((line=input.readLine()) != null) {
		    	            System.out.println(line);
		    	        }
		    	
		    	        int exitVal = pr.waitFor();
//		    	        System.out.println("Exited with error code "+exitVal);
		    	
		    	    } catch(Exception e) {
		    	        System.out.println(e.toString());
		    	        e.printStackTrace();
		    	    }
		        }
		    }
		} catch (Exception e) {
			System.out.println( "Invalid File Data:\n" + e.getMessage() + " at " + fileName + ".dot");
		}
		
	}

}